package stu.day33_day38.day33;

import java.util.Scanner;

public class hw1 {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] arr = new int[m][n];

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                arr[i][j] = sc.nextInt();
            }
        }

        int max = 0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(arr[i][j]>=0){
                    max = Math.max(max,dfs(i,j,m,n,arr[i][j],arr));
                }
            }
        }
        System.out.println(max);
    }

    public static int dfs(int i,int j,int m,int n,int num,int[][] arr){
        if(i>=0&&i<m&&j>=0&&j<n){
            if(Math.abs(arr[i][j]-num)<=1){
                num = arr[i][j];
                arr[i][j] = -1;
                return 1+dfs(i+1,j,m,n,num,arr)+dfs(i-1,j,m,n,num,arr)+dfs(i,j+1,m,n,num,arr)+dfs(i,j-1,m,n,num,arr);
            }
        }
        return 0;
    }


}
